#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_RECORDS 100
#define FILENAME "data.bin"

typedef struct {
    int id;
    char name[50];
    int age;
    char email[100];
} User;

User database[MAX_RECORDS];
int record_count = 0;

// 保存数据到文件
void save_to_file() {
    FILE *file = fopen(FILENAME, "wb");
    if (file) {
        fwrite(&record_count, sizeof(int), 1, file);
        fwrite(database, sizeof(User), record_count, file);
        fclose(file);
    }
}

// 从文件加载数据
void load_from_file() {
    FILE *file = fopen(FILENAME, "rb");
    if (file) {
        fread(&record_count, sizeof(int), 1, file);
        fread(database, sizeof(User), record_count, file);
        fclose(file);
    }
}

// 添加用户
void add_user(const char *name, int age, const char *email) {
    if (record_count < MAX_RECORDS) {
        User *user = &database[record_count];
        user->id = record_count + 1;
        strncpy(user->name, name, sizeof(user->name));
        user->age = age;
        strncpy(user->email, email, sizeof(user->email));
        record_count++;
        save_to_file();
        printf("用户添加成功\n");
    } else {
        printf("数据库已满\n");
    }
}

// 显示所有用户
void show_all_users() {
    printf("所有用户:\n");
    for (int i = 0; i < record_count; i++) {
        printf("ID: %d, Name: %s, Age: %d, Email: %s\n", 
               database[i].id, database[i].name, database[i].age, database[i].email);
    }
}

int main() {
    load_from_file();
    
    // 示例操作
    add_user("张三", 25, "zhangsan@email.com");
    add_user("李四", 30, "lisi@email.com");
    
    show_all_users();
    
    return 0;
}